package com.uchengwang.ui.cm_ui.ucity.ui;

import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;

import com.chanven.lib.cptr.PtrClassicFrameLayout;
import com.chanven.lib.cptr.PtrDefaultHandler;
import com.chanven.lib.cptr.PtrFrameLayout;
import com.chanven.lib.cptr.loadmore.OnLoadMoreListener;
import com.chanven.lib.cptr.recyclerview.RecyclerAdapterWithHF;
import com.uchengwang.R;
import com.uchengwang.functions.http.config.BaseConfig;
import com.uchengwang.ui.base_ui.ui.BaseWithTitleActivity;
import com.uchengwang.ui.cm_ui.login.bean.UserBean;
import com.uchengwang.ui.cm_ui.ucity.adapter.ApplyAdapter;
import com.uchengwang.ui.cm_ui.ucity.listener.GetApplyListBeanListener;
import com.uchengwang.ui.cm_ui.ucity.presenter.HttpCityPresenter;
import com.uchengwang.ui.utils.UCommonUtil;

import java.util.List;

/**
 * Created by U城网 on 2017/7/19.
 * 在未登录下, 查看某个活动的报名人数
 */

public class CheckApplyActivity extends BaseWithTitleActivity{
    private RecyclerView recycleView;
    private PtrClassicFrameLayout mSwipeRefreshLayout;
    private RecyclerAdapterWithHF mAdapter;
    private ApplyAdapter adapter;

    private int page= 1, totlaPagers;
    private long id;//活动的ID

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_ustudy_2);
        id = getIntent().getLongExtra(BaseConfig.ID, -1);
        setTitle("已报名");

        initView();
    }

    private void initView(){
        recycleView = (RecyclerView) findViewById(R.id.u_study_recycleView);
        mSwipeRefreshLayout = (PtrClassicFrameLayout) findViewById(R.id.mRefreshLayout);

        initData();
    }

    private void initData(){
        adapter = new ApplyAdapter(this, false);

        mAdapter = new RecyclerAdapterWithHF(adapter);
        recycleView.setLayoutManager(new LinearLayoutManager(this));
        recycleView.setAdapter(mAdapter);

        mSwipeRefreshLayout.setAutoLoadMoreEnable(true);
        mSwipeRefreshLayout.setLoadMoreEnable(true);

        mSwipeRefreshLayout.postDelayed(new Runnable() {

            @Override
            public void run() {
                mSwipeRefreshLayout.autoRefresh(true);
            }
        }, 150);

        mSwipeRefreshLayout.setPtrHandler(new PtrDefaultHandler() {

            @Override
            public void onRefreshBegin(PtrFrameLayout frame) {
                page = 1;
                mSwipeRefreshLayout.loadMoreComplete(true);
                getApplys(true);
            }
        });

        mSwipeRefreshLayout.setOnLoadMoreListener(new OnLoadMoreListener() {

            @Override
            public void loadMore() {
                page++;
                if (page <= totlaPagers){
                    //当前请求页大于总页数，不再请求
                    getApplys(false);
                } else {
                    mSwipeRefreshLayout.loadMoreComplete(false);
                }
            }
        });
    }

    /**
     * 获取报名人数信息
     * */
    private void getApplys(final boolean isRefresh){
        HttpCityPresenter.getPresenter().getApplyInfos(this, id, page, 20, null,  new GetApplyListBeanListener() {
            @Override
            public void onResult(int status, int allPagers, int total, String msg, List<UserBean> data) {
                if (isRefresh){
                    mSwipeRefreshLayout.refreshComplete();
                }else {
                    mSwipeRefreshLayout.loadMoreComplete(true);
                }
                if (status != 0 || data == null){
                    UCommonUtil.getUtil().showToast(CheckApplyActivity.this, msg);
                    return;
                }
                totlaPagers = allPagers;
                if (isRefresh){
                    adapter.setData(data);
                }else {
                    adapter.getData().addAll(data);
                    adapter.notifyDataSetChanged();
                }
            }
        });


    }

}
